import time
from public.action import *
from public.business import *
from public.testData import *
from public.tools import *


testcase = "dbshop004_取消订单"

# 登录官网
firefox = openBrowser(url)
DBshopLogin(firefox,"zhangsan","zhangsan")

# 1.在首页搜索"小米",点击搜索按钮
firefox.find_element_by_xpath('//input[@class="search-text"]').send_keys("小米")
firefox.find_element_by_xpath('//input[@class="search-btn iconfont"]').click()

# 2.选择小米mix3,加入到购物车,点击购物车按钮
#下拉右侧滚动条
firefox.execute_script("window.scrollTo(0,500)")
priceElement=firefox.find_element_by_id('goods_price_8')
#调用自定义函数进行模拟鼠标移动
moveToElement(firefox,priceElement)

#点击加入购物车按钮
firefox.find_element_by_xpath('//a[@onclick="add_cart(8, 2);"]').click()
time.sleep(5)
# 点击购物车
firefox.find_element_by_id("J_miniCart").click()

# 3.点击"去结算"按钮
firefox.find_element_by_xpath('//a[text()="去结算"]').click()

# 4.默认地址已经选择,点击下一步按钮
firefox.find_element_by_xpath('//input[@value="下一步"]').click()

# 5.支付方式选择"余额支付",点击确认订单
firefox.find_element_by_xpath('//input[@value="yezf"]').click()
firefox.find_element_by_xpath('//input[@value="确认订单"]').click()

# 保存订单编号
orderNO = firefox.find_element_by_xpath('(//font[@color="color:#ED145B"])[2]').text
rootPath=get_pro_rootpath()
writeFile(rootPath+'\public\orderID.txt',orderNO) # 将订单编号写入到文件中

# 6.点击"查看订单详情",
firefox.find_element_by_xpath('//input[@value="查看订单详情"]').click()
# 点击"取消订单"
firefox.find_element_by_xpath('//input[@name="search_order_sn"]').send_keys(orderNO)
firefox.find_element_by_xpath('//button[@class="btn btn-small btn-primary"]').click()

# 点击取消
firefox.find_element_by_xpath('//button[text()="取消订单"]').click()
firefox.switch_to.alert.accept()  #点击确认

# 断言
firefox.find_element_by_xpath('//a[@href="/dbshop/home/order/index/state/0"]').click()
time.sleep(5)
firefox.find_element_by_xpath('//input[@name="search_order_sn"]').send_keys(orderNO)
firefox.find_element_by_xpath('//button[@class="btn btn-small btn-primary"]').click()
time.sleep(5)
try:
    firefox.find_element_by_link_text(orderNO).is_displayed()
    writeLog("%s 测试通过"%testcase)
except:
    writeLog("%s 测试失败" % testcase)
    getPhoto(firefox)
firefox.quit()







